package plp.imperative2.declaration;

import plp.expressions2.expression.Expressao;
import plp.expressions2.expression.Valor;
import plp.expressions2.expression.ValorBooleano;
import plp.expressions2.expression.ValorInteiro;
import plp.expressions2.memory.AmbienteExecucao;
import plp.expressions2.memory.IdentificadorJaDeclaradoException;
import plp.expressions2.memory.IdentificadorNaoDeclaradoException;
import plp.expressions2.memory.VariavelJaDeclaradaException;
import plp.expressions2.memory.VariavelNaoDeclaradaException;
import plp.imperative1.memory.AmbienteCompilacaoImperativa;
import plp.imperative1.memory.EntradaVaziaException;
import plp.imperative2.memory.ContextoExecucaoImperativa2;
import plp.imperative2.memory.DurException;

public class DefDurDbc extends DefPosDbc {

	public DefDurDbc(Expressao condicao) {
		super(condicao);
	}

	Valor avaliar(AmbienteExecucao amb) throws VariavelNaoDeclaradaException,
			VariavelJaDeclaradaException, DurException {

		long tempoExecucao = ((ContextoExecucaoImperativa2) amb)
				.getTempoExecucao();
		ValorInteiro valorTempo = (ValorInteiro) condicao.avaliar(amb);

		if (valorTempo.valor() >= tempoExecucao) {
			return new ValorBooleano(true);
		} else {
			throw new DurException(tempoExecucao);
		}
	}

	public boolean checaTipo(AmbienteCompilacaoImperativa ambiente)
			throws IdentificadorJaDeclaradoException,
			IdentificadorNaoDeclaradoException, EntradaVaziaException {

		return condicao.checaTipo(ambiente)
				&& condicao.getTipo(ambiente).eInteiro();
	}

}